package com.biz.controller.weixin;

import com.biz.service.weixin.WXService;
import com.biz.service.weixin.WXUtilService;
import com.biz.utils.ConfigUtil;
import com.framework.controller.BaseController;
import com.framework.util.weixin.AesException;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;


/**
 * 注解方式打开链接
 * 
 * @author GengLong
 *
 */
@Controller
public class WXController extends BaseController
{
	@Resource(name="wxUtilService")
	private WXUtilService wxUtilService;
	
	@Resource(name = "wxService")
	private WXService wxService;

	@RequestMapping(value = { "/wx_index" }, method = RequestMethod.GET)
	public void WXGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException
	{
		// 微信加密签名
		String signature = request.getParameter("signature");
		// 时间戳
		String timestamp = request.getParameter("timestamp");
		// 随机数
		String nonce = request.getParameter("nonce");
		// 随机字符串
		String echostr = request.getParameter("echostr");
		//System.out.println("request=" + request.getRequestURL());

		PrintWriter out = response.getWriter();
		// 通过检验signature对请求进行校验，若校验成功则原样返回echostr，表示接入成功，否则接入失败
		String result = null;
		try
		{
			result = wxService.verifyUrl(signature, ConfigUtil.get("TOKEN"), timestamp,
					nonce, echostr);
		} catch (AesException e)
		{
			e.printStackTrace();
		}
		if (result == null)
		{
			result = ConfigUtil.get("TOKEN");
		}
		out.print(result);
		out.close();
		out = null;
	}

	@RequestMapping(value = { "/wx_index" }, method = RequestMethod.POST)
	public void WXPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException
	{
		// 将请求、响应的编码均设置为UTF-8（防止中文乱码）
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		// 从请求中读取整个post数据
		InputStream inputStream = request.getInputStream();
		String msg = IOUtils.toString(inputStream, "UTF-8");
		System.out.println("msg:" + msg);

		// 调用核心业务类接收消息、处理消息
		String respMessage = wxService.processRequest(msg);
		System.out.println("respMessage=" + respMessage);

		// 响应消息
		PrintWriter out = response.getWriter();
		out.print(respMessage);
		out.close();
	}

	/**
	 * 获得js接口数据
	 *
	 * @param url
	 * @return
	 */
	@RequestMapping(value = "wx_js")
	public void getJsData(String url ,HttpServletResponse response)
	{
		String appid = ConfigUtil.get("appId");

		Map<String, Object> map = new HashMap<String, Object>();
		String jsapi_ticket = wxUtilService.getJsApiTicket();
		map.put("jsData", WXSign.sign(jsapi_ticket, url,appid));
        System.out.println("wx_js=========================================>"+map);
		writeJson(map,response);
	}

}
